package com.csyf.manager.controller;

import com.csyf.manager.entity.SysAgent;
import com.csyf.manager.entity.SysMerchant;
import com.csyf.manager.entity.SysUser;
import com.csyf.manager.service.SysAgentService;
import com.csyf.manager.service.SysMerchBiz;
import com.csyf.manager.service.SysUserService;
import com.csyf.manager.service.impl.RedisService;
import com.csyf.manager.utils.NumberHelpler;
import com.csyf.manager.utils.ReturnData;
import com.csyf.manager.utils.SmsHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

/**
 * 短信验证码接口
 */
@Controller
@RequestMapping(value = "sys/sms")
public class SmsController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private SysMerchBiz sysMerchBiz;


    @Autowired
    private SysAgentService sysAgentService;


    @Autowired
    private SysUserService sysUserService;

    /**
     * 发送短信验证码
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "sendVerifyCode")
    public ReturnData sendVerifyCode(String money) {
        ReturnData returnData = new ReturnData();
        SysMerchant userInfo = getUserInfo();

        if (null == userInfo) {
            returnData.setCode(10);
            returnData.setMsg("登录过期，请重新登录！");
            return returnData;
        }

        try {
            //获取商户预留手机号
            SysMerchant agent = sysMerchBiz.queryAgentByMerchantNo(userInfo.getMerchMerchantNo());
            String reservedPhone = agent.getMerchPhone();

            String randomCode = NumberHelpler.getFixLenRandom(6);
            //发送短信验证码
            String verifyCode = SmsHelper.sendMsg(reservedPhone, randomCode, money);
            //将验证码缓存到redis中
            redisService.setLoginSmsVerifyCode(reservedPhone, verifyCode);

            String returnMsg = reservedPhone.substring(7, reservedPhone.length());
            returnData.setCode(0);
            returnData.setMsg("验证码已发送到您尾号" + returnMsg + "的手机，请注意查收");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            returnData.setCode(10);
            returnData.setMsg("获取验证码失败，请联系技术");
        }
        return returnData;
    }


    /**
     * 发送登录验证码
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "sendLoginVerifyCode")
    public ReturnData sendLoginVerifyCode(String phone) {
        ReturnData returnData = new ReturnData();

        if (StringUtils.isBlank(phone)) {
            returnData.setCode(10);
            returnData.setMsg("");
            return returnData;
        }


        try {

            SysUser userInfo = sysUserService.selectByUsername(phone);

            if (null == userInfo) {
                returnData.setCode(10);
                returnData.setMsg("用户不存在，请重新输入");
                return returnData;
            }
            //获取商户预留手机号
            String reservedPhone = "";
            if (userInfo.getUserId() == 1) {
                reservedPhone = "15701510275;18310139974;15701276032";
            } else {
                if("2".equals(userInfo.getUserType())){
                    SysAgent sysAgent = sysAgentService.queryAgentByUserId(userInfo.getUserId());
                    reservedPhone = sysAgent.getAgentPhone();
                }else if("3".equals(userInfo.getUserType())){
                    SysMerchant agent = sysMerchBiz.queryAgentByUserId(userInfo.getUserId());
                    reservedPhone = agent.getMerchPhone();
                }
            }


            String randomCode = NumberHelpler.getFixLenRandom(6);
            //发送短信验证码
            String verifyCode = SmsHelper.sendlogMsg(reservedPhone, randomCode);
            //将验证码缓存到redis中
            redisService.setLoginSmsVerifyCode(reservedPhone, verifyCode);

            String returnMsg = reservedPhone.substring(reservedPhone.length() - 4, reservedPhone.length());
            returnData.setCode(0);
            returnData.setMsg("验证码已发送到您尾号" + returnMsg + "的手机，请注意查收");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            returnData.setCode(10);
            returnData.setMsg("获取验证码失败，请联系技术");
        }
        return returnData;
    }

    private SysMerchant getUserInfo() {
        return (SysMerchant) httpSession.getAttribute("login");
    }


}
